package uk.co.hephistocles.blobGame;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Decompress {
  
public static void unzip(InputStream src, String dest){
  
  final int BUFFER_SIZE = 4096;
  
  BufferedOutputStream bufferedOutputStream = null;
     try {
      ZipInputStream zipInputStream 
       = new ZipInputStream(src);
      ZipEntry zipEntry;
      
      while ((zipEntry = zipInputStream.getNextEntry()) != null){
       
       String zipEntryName = zipEntry.getName();
       File file = new File(dest + zipEntryName);
       
       if (file.exists()){
        
       } else {
        if(zipEntry.isDirectory()){
         file.mkdirs(); 
        }else{
         byte buffer[] = new byte[BUFFER_SIZE];
         FileOutputStream fileOutputStream = new FileOutputStream(file);
         bufferedOutputStream 
          = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE);
         int count;

         while ((count 
          = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
          bufferedOutputStream.write(buffer, 0, count);
         }

         bufferedOutputStream.flush();
         bufferedOutputStream.close(); 
        }
       } 
      }
      zipInputStream.close();
     } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
 }
}